package com.ruoyi.pay.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDnS97J1eX2pXu+0E0ksZCN9hw8sWUkaXRa2H33JODPOO/YdfOqa64tPLSG/+s82l/gA49Hv5S/29k/ptkBfuPYO9KDDGBZd08No5uqJNUikZG3Cg3f3bcOAv5DhWfDyWLrwAS48Vp5DFCvjhkmrpyQu6nmqEquHARy1zsKLhwy5HyBKAFMJng5oXrP7w7+qALaIGBS8bXfhnRz2rhpe1mZSH5OWPyQUQuUf0Fp5DRJkyuU/x5ebatyvdqX1tuho7nT86nXGDbuI6a6doez//P2zSL3Hg6+E2Jv5m0F6NNwAuLq2ZW8hMvYzQNSj/HtO1+tkCTy1SiEdrTHMYZYnXS7AgMBAAECggEAGPDyfjtFh/pCRMo7d1h6aQR/+P98E5gXhvBPbQYaA9505peZhegEqTYhlWklWbOGoSZhHfCHvsSbQaThQ6sGMLwKsDz2as9Vw/e/gnJkVUzzNJSt+FT1N1qVRnytOsyNkTRsjfDgu6rbTIL7LVOo6GaEJK5DF1FIHYYeBLQCMG6EgdrWnDKxnTcJ+sivvlLR3ea7CKV+utwpJs2PMZ/KvCSZcDWF1qdeq9nDZz236Zjn4KDpHPMpKFOo1rGslKU9AUQZRUp1YpkCYvYjqq5s3numrqwM128jPPjzbAJ5mdICLP0qgrNXyUfmj6vxCZ+zTv87y2mxDNHI0hejHJW3KQKBgQ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k2O13eDVT/4W1oWu1KA34QmDZqjJxBzdR8q9FEPDzhAtiOVIILI3IrwvVqQX8IZYPIBdcGibaytNrAplp70dGOjgVoRD8QToAQA5+MA0T2lAzDPn5wLH1VQAatRdAsUlSrmsYDQUVid2LDWOJG0Ue4a0iNkF7LOqPz3zLVm1mJEHeTCrlro0h+h+Dut6MqumwIDAQAB";
    //生成支付订单接口
    public Map pay(String orderId, String price) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000122669503");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl("http://localhost:9302/alipayExam/result");



        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(orderId);
        model.setTotalAmount(price);
        model.setSubject("拼夕夕商城订单");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000122669503");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
